/*
 * 题库
 */

import { createAPI } from '@/utils/request'

// 基础题库列表
// export const getQuestionList = data => createAPI('/questions', 'get', data)
export const getQuestionList = function(data) {
  const questionTypeMapping = new Map([
    ['2', '多选'],
    ['1', '单选'],
    ['3', '简答']
  ])
  const difficultyMapping = new Map([
    ['3', '困难'],
    ['2', '一般'],
    ['1', '简单']
  ])
  return createAPI('/questions', 'get', data)
    .then(res => ({
      counts: res.data.counts,
      items: res.data.items.map((d, i) => ({
        ...d,
        addDate: d.addDate.substring(0, 19).replace('T', ' '),
        // index: (data.page - 1) * data.pagesize + i + 1,
        difficulty: difficultyMapping.get(d.difficulty),
        questionType: questionTypeMapping.get(d.questionType),
      })),
    }))
    .catch(err => err)
}


// 基础题库详情
// export const detail = data => createAPI(`/questions/${data.id}`, 'get', data)
export const detail = function(data) {
  const questionTypeMapping = new Map([
    ['2', '多选'],
    ['1', '单选'],
    ['3', '简答']
  ])
  const difficultyMapping = new Map([
    ['3', '困难'],
    ['2', '一般'],
    ['1', '简单']
  ])
  return createAPI(`/questions/${data.id}`, 'get', data)
    .then(res => ({
      ...res.data,
      questionType: questionTypeMapping.get(res.data.questionType),
      difficulty: difficultyMapping.get(res.data.difficulty),
    }))
    .catch(err => err)
}


// 批量导入题
export const batch = data => createAPI('/questions/batch', 'post', data)

// 基础题库添加
export const add = data => createAPI('/questions', 'post', data)

// 基础题库修改
export const update = data => createAPI(`/questions/${data.id}`, 'put', data)

// 基础题库删除
export const remove = data => createAPI(`/questions/${data.id}`, 'delete', data)

// 精选题库加入取消
export const choiceAdd = data =>
  createAPI(`/questions/choice/${data.id}/${data.choiceState}`, 'patch', data)

// 精选题库列表
// export const choice = data => createAPI('/questions/choice', 'get', data)
export const choice = function (data) {
  const questionTypeMapping = new Map([
    ['2', '多选'],
    ['1', '单选'],
    ['3', '简答']
  ])
  const difficultyMapping = new Map([
    ['3', '困难'],
    ['2', '一般'],
    ['1', '简单']
  ])
  const chkStateMapping = new Map([
    [0, '待审核'],
    [1, '已审核'],
    [2, '已拒绝']
  ])
  const publishStateMapping = new Map([
    [0, '已下架'],
    [1, '已上架']
  ])
  return createAPI('/questions/choice', 'get', data)
    .then(res => ({
      counts: res.data.counts,
      items: res.data.items.map((d, i) => ({
        ...d,
        addDate: d.addDate.substring(0, 19).replace('T', ' '),
        // index: (data.page - 1) * data.pagesize + i + 1,
        difficulty: difficultyMapping.get(d.difficulty),
        questionType: questionTypeMapping.get(d.questionType),
        publishState: publishStateMapping.get(d.publishState),
        chkState: chkStateMapping.get(d.chkState),
      })),
    }))
    .catch(err => err)
}


// 精选题库上下架
export const choicePublish = data =>
  createAPI(`/questions/choice/${data.id}/${data.publishState}`, 'post', data)

// 试题审核
export const choiceCheck = data =>
  createAPI(`/questions/check/${data.id}`, 'post', data)

// 试题审核意见列表
export const auditOpinions = data =>
  createAPI(`/questions/${data.id}/auditOpinions`, 'get', data)

// 出题记录
export const setRecords = data =>
  createAPI(`/questions/${data.id}/setRecords`, 'get', data)

// 组题列表
export const randoms = data => createAPI('/questions/randoms', 'get', data)

// 组题列表删除
export const removeRandoms = data =>
  createAPI(`/questions/randoms/${data.id}`, 'delete', data)
